home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / vc.el < prev    next >
Lisp/Scheme  |  1993-11-26  |  76KB  |  2,097 lines

  1. ;;; vc.el --- drive a version-control system from within Emacs
  2.  
  3. ;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
  6. ;; Version: 5.4
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This mode is fully documented in the Emacs user's manual.
  27. ;;
  28. ;; This was designed and implemented by Eric Raymond <esr@snark.thyrsus.com>.
  29. ;; Paul Eggert <eggert@twinsun.com>, Sebastian Kremer <sk@thp.uni-koeln.de>,
  30. ;; and Richard Stallman contributed valuable criticism, support, and testing.
  31. ;;
  32. ;; Supported version-control systems presently include SCCS and RCS;
  33. ;; your RCS version should be 5.6.2 or later for proper operation of
  34. ;; the lock-breaking code.
  35. ;;
  36. ;; The RCS code assumes strict locking.  You can support the RCS -x option
  37. ;; by adding pairs to the vc-master-templates list.
  38. ;;
  39. ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
  40. ;; to be installed somewhere on Emacs's path for executables.
  41. ;;
  42. ;; If your site uses the ChangeLog convention supported by Emacs, the
  43. ;; function vc-comment-to-change-log should prove a useful checkin hook.
  44. ;;
  45. ;; This code depends on call-process passing back the subprocess exit
  46. ;; status.  Thus, you need Emacs 18.58 or later to run it.  For the
  47. ;; vc-directory command to work properly as documented, you need 19.
  48. ;; You also need Emacs 19's ring.el.
  49. ;;
  50. ;; The vc code maintains some internal state in order to reduce expensive
  51. ;; version-control operations to a minimum.  Some names are only computed
  52. ;; once.  If you perform version control operations with RCS/SCCS/CVS while
  53. ;; vc's back is turned, or move/rename master files while vc is running,
  54. ;; vc may get seriously confused.  Don't do these things!
  55. ;;
  56. ;; Developer's notes on some concurrency issues are included at the end of
  57. ;; the file.
  58.  
  59. ;;; Code:
  60.  
  61. (require 'vc-hooks)
  62. (require 'ring)
  63.  
  64. (if (not (assoc 'vc-parent-buffer minor-mode-alist))
  65.     (setq minor-mode-alist
  66.       (cons '(vc-parent-buffer vc-parent-buffer-name)
  67.         minor-mode-alist)))
  68.  
  69. ;; General customization
  70.  
  71. (defvar vc-default-back-end nil
  72.   "*Back-end actually used by this interface; may be SCCS or RCS.
  73. The value is only computed when needed to avoid an expensive search.")
  74. (defvar vc-suppress-confirm nil
  75.   "*If non-nil, treat user as expert; suppress yes-no prompts on some things.")
  76. (defvar vc-keep-workfiles t
  77.   "*If non-nil, don't delete working files after registering changes.")
  78. (defvar vc-initial-comment nil
  79.   "*Prompt for initial comment when a file is registered.")
  80. (defvar vc-command-messages nil
  81.   "*Display run messages from back-end commands.")
  82. (defvar vc-mistrust-permissions 'file-symlink-p
  83.   "*Don't assume that permissions and ownership track version-control status.")
  84. (defvar vc-checkin-switches nil
  85.   "*Extra switches passed to the checkin program by \\[vc-checkin].")
  86. (defvar vc-path
  87.   (if (file-exists-p "/usr/sccs")
  88.       '("/usr/sccs") nil)
  89.   "*List of extra directories to search for version control commands.")
  90.  
  91. (defconst vc-maximum-comment-ring-size 32
  92.   "Maximum number of saved comments in the comment ring.")
  93.  
  94. ;;; This is duplicated in diff.el.
  95. (defvar diff-switches "-c"
  96.   "*A string or list of strings specifying switches to be be passed to diff.")
  97.  
  98. ;;;###autoload
  99. (defvar vc-checkin-hook nil
  100.   "*List of functions called after a checkin is done.  See `run-hooks'.")
  101.  
  102. ;; Header-insertion hair
  103.  
  104. (defvar vc-header-alist
  105.   '((SCCS "\%W\%") (RCS "\$Id\$"))
  106.   "*Header keywords to be inserted when `vc-insert-headers' is executed.")
  107. (defvar vc-static-header-alist
  108.   '(("\\.c$" .
  109.      "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n#endif /* lint */\n"))
  110.   "*Associate static header string templates with file types.  A \%s in the
  111. template is replaced with the first string associated with the file's
  112. version-control type in `vc-header-alist'.")
  113.  
  114. (defvar vc-comment-alist
  115.   '((nroff-mode ".\\\"" ""))
  116.   "*Special comment delimiters to be used in generating vc headers only.
  117. Add an entry in this list if you need to override the normal comment-start
  118. and comment-end variables.  This will only be necessary if the mode language
  119. is sensitive to blank lines.")
  120.  
  121. ;; Default is to be extra careful for super-user.
  122. (defvar vc-checkout-carefully (= (user-uid) 0)
  123.   "*Non-nil means be extra-careful in checkout.
  124. Verify that the file really is not locked
  125. and that its contents match what the master file says.")
  126.  
  127. ;; Variables the user doesn't need to know about.
  128. (defvar vc-log-entry-mode nil)
  129. (defvar vc-log-operation nil)
  130. (defvar vc-log-after-operation-hook nil)
  131. (defvar vc-checkout-writable-buffer-hook 'vc-checkout-writable-buffer)
  132. (defvar vc-parent-buffer nil)
  133. (defvar vc-parent-buffer-name nil)
  134.  
  135. (defvar vc-log-file)
  136. (defvar vc-log-version)
  137.  
  138. (defconst vc-name-assoc-file "VC-names")
  139.  
  140. (defvar vc-dired-mode nil)
  141. (make-variable-buffer-local 'vc-dired-mode)
  142.  
  143. (defvar vc-comment-ring nil)
  144. (defvar vc-comment-ring-index nil)
  145. (defvar vc-last-comment-match nil)
  146.  
  147. ;; File property caching
  148.  
  149. (defun vc-file-clearprops (file)
  150.   ;; clear all properties of a given file
  151.   (setplist (intern file vc-file-prop-obarray) nil))
  152.  
  153. (defun vc-clear-context ()
  154.   "Clear all cached file properties and the comment ring."
  155.   (interactive)
  156.   (fillarray vc-file-prop-obarray nil)
  157.   ;; Note: there is potential for minor lossage here if there is an open
  158.   ;; log buffer with a nonzero local value of vc-comment-ring-index.
  159.   (setq vc-comment-ring nil))
  160.  
  161. ;; Random helper functions
  162.  
  163. (defun vc-registration-error (file)
  164.   (if file
  165.       (error "File %s is not under version control" file)
  166.     (error "Buffer %s is not associated with a file" (buffer-name))))
  167.  
  168. (defvar vc-binary-assoc nil)
  169.  
  170. (defun vc-find-binary (name)
  171.   "Look for a command anywhere on the subprocess-command search path."
  172.   (or (cdr (assoc name vc-binary-assoc))
  173.       (catch 'found
  174.     (mapcar
  175.      (function 
  176.       (lambda (s)
  177.         (if s
  178.         (let ((full (concat s "/" name)))
  179.           (if (file-executable-p full)
  180.               (progn
  181.             (setq vc-binary-assoc
  182.                   (cons (cons name full) vc-binary-assoc))
  183.             (throw 'found full)))))))
  184.      exec-path)
  185.     nil)))
  186.  
  187. (defun vc-do-command (okstatus command file &rest flags)
  188.   "Execute a version-control command, notifying user and checking for errors.
  189. The command is successful if its exit status does not exceed OKSTATUS.
  190. Output from COMMAND goes to buffer *vc*.  The last argument of the command is
  191. the master name of FILE; this is appended to an optional list of FLAGS."
  192.   (setq file (expand-file-name file))
  193.   (if vc-command-messages
  194.       (message "Running %s on %s..." command file))
  195.   (let ((obuf (current-buffer)) (camefrom (current-buffer))
  196.     (squeezed nil)
  197.     (vc-file (and file (vc-name file)))
  198.     status)
  199.     (set-buffer (get-buffer-create "*vc*"))
  200.     (set (make-local-variable 'vc-parent-buffer) camefrom)
  201.     (set (make-local-variable 'vc-parent-buffer-name)
  202.      (concat " from " (buffer-name camefrom)))
  203.     
  204.     (erase-buffer)
  205.  
  206.     ;; This is so that command arguments typed in the *vc* buffer will
  207.     ;; have reasonable defaults.
  208.     (setq default-directory (file-name-directory file))
  209.  
  210.     (mapcar
  211.      (function (lambda (s) (and s (setq squeezed (append squeezed (list s))))))
  212.      flags)
  213.     (if vc-file
  214.     (setq squeezed (append squeezed (list vc-file))))
  215.     (let ((default-directory (file-name-directory (or file "./")))
  216.       (exec-path (if vc-path (append exec-path vc-path) exec-path)))
  217.       (setq status (apply 'call-process command nil t nil squeezed)))
  218.     (goto-char (point-max))
  219.     (forward-line -1)
  220.     (if (or (not (integerp status)) (< okstatus status))
  221.     (progn
  222.       (pop-to-buffer "*vc*")
  223.       (goto-char (point-min))
  224.       (shrink-window-if-larger-than-buffer)
  225.       (error "Running %s...FAILED (%s)" command
  226.          (if (integerp status)
  227.              (format "status %d" status)
  228.            status))
  229.       )
  230.       (if vc-command-messages
  231.       (message "Running %s...OK" command))
  232.       )
  233.     (set-buffer obuf)
  234.     status)
  235.   )
  236.  
  237. ;;; Save a bit of the text around POSN in the current buffer, to help
  238. ;;; us find the corresponding position again later.  This works even
  239. ;;; if all markers are destroyed or corrupted.
  240. (defun vc-position-context (posn)
  241.   (list posn
  242.     (buffer-size)
  243.     (buffer-substring posn
  244.               (min (point-max) (+ posn 100)))))
  245.  
  246. ;;; Return the position of CONTEXT in the current buffer, or nil if we
  247. ;;; couldn't find it.
  248. (defun vc-find-position-by-context (context)
  249.   (let ((context-string (nth 2 context)))
  250.     (if (equal "" context-string)
  251.     (point-max)
  252.       (save-excursion
  253.     (let ((diff (- (nth 1 context) (buffer-size))))
  254.       (if (< diff 0) (setq diff (- diff)))
  255.       (goto-char (nth 0 context))
  256.       (if (or (search-forward context-string nil t)
  257.           ;; Can't use search-backward since the match may continue
  258.           ;; after point.
  259.           (progn (goto-char (- (point) diff (length context-string)))
  260.              ;; goto-char doesn't signal an error at
  261.              ;; beginning of buffer like backward-char would
  262.              (search-forward context-string nil t)))
  263.           ;; to beginning of OSTRING
  264.           (- (point) (length context-string))))))))
  265.  
  266. (defun vc-revert-buffer1 (&optional arg no-confirm)
  267.   ;; Most of this was shamelessly lifted from Sebastian Kremer's rcs.el mode.
  268.   ;; Revert buffer, try to keep point and mark where user expects them in spite
  269.   ;; of changes because of expanded version-control key words.
  270.   ;; This is quite important since otherwise typeahead won't work as expected.
  271.   (interactive "P")
  272.   (widen)
  273.   (let ((point-context (vc-position-context (point)))
  274.     ;; Use mark-marker to avoid confusion in transient-mark-mode.
  275.     (mark-context  (if (eq (marker-buffer (mark-marker)) (current-buffer))
  276.                (vc-position-context (mark-marker))))
  277.     ;; Make the right thing happen in transient-mark-mode.
  278.     (mark-active nil)
  279.     ;; We may want to reparse the compilation buffer after revert
  280.     (reparse (and (boundp 'compilation-error-list) ;compile loaded
  281.               (let ((curbuf (current-buffer)))
  282.             ;; Construct a list; each elt is nil or a buffer
  283.             ;; iff that buffer is a compilation output buffer
  284.             ;; that contains markers into the current buffer.
  285.             (save-excursion
  286.               (mapcar (function
  287.                    (lambda (buffer)
  288.                     (set-buffer buffer)
  289.                     (let ((errors (or
  290.                            compilation-old-error-list
  291.                            compilation-error-list))
  292.                       (buffer-error-marked-p nil))
  293.                       (while (and (consp errors)
  294.                           (not buffer-error-marked-p))
  295.                     (and (markerp (cdr (car errors)))
  296.                          (eq buffer
  297.                          (marker-buffer
  298.                           (cdr (car errors))))
  299.                          (setq buffer-error-marked-p t))
  300.                     (setq errors (cdr errors)))
  301.                       (if buffer-error-marked-p buffer))))
  302.                   (buffer-list)))))))
  303.  
  304.     ;; the actual revisit
  305.     (revert-buffer arg no-confirm)
  306.  
  307.     ;; Reparse affected compilation buffers.
  308.     (while reparse
  309.       (if (car reparse)
  310.       (save-excursion
  311.         (set-buffer (car reparse))
  312.         (let ((compilation-last-buffer (current-buffer)) ;select buffer
  313.           ;; Record the position in the compilation buffer of
  314.           ;; the last error next-error went to.
  315.           (error-pos (marker-position
  316.                   (car (car-safe compilation-error-list)))))
  317.           ;; Reparse the error messages as far as they were parsed before.
  318.           (compile-reinitialize-errors '(4) compilation-parsing-end)
  319.           ;; Move the pointer up to find the error we were at before
  320.           ;; reparsing.  Now next-error should properly go to the next one.
  321.           (while (and compilation-error-list
  322.               (/= error-pos (car (car compilation-error-list))))
  323.         (setq compilation-error-list (cdr compilation-error-list))))))
  324.       (setq reparse (cdr reparse)))
  325.  
  326.     ;; Restore point and mark
  327.     (let ((new-point (vc-find-position-by-context point-context)))
  328.       (if new-point (goto-char new-point)))
  329.     (if mark-context
  330.     (let ((new-mark (vc-find-position-by-context mark-context)))
  331.       (if new-mark (set-mark new-mark))))))
  332.  
  333.  
  334. (defun vc-buffer-sync ()
  335.   ;; Make sure the current buffer and its working file are in sync
  336.   (if (buffer-modified-p)
  337.       (progn
  338.     (or vc-suppress-confirm
  339.         (y-or-n-p (format "Buffer %s modified; save it? " (buffer-name)))
  340.         (error "Aborted"))
  341.     (save-buffer))))
  342.  
  343. (defun vc-workfile-unchanged-p (file)
  344.   ;; Has the given workfile changed since last checkout?
  345.   (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
  346.     (lastmod (nth 5 (file-attributes file))))
  347.     (if checkout-time
  348.      (equal lastmod checkout-time)
  349.      (if (zerop (vc-backend-diff file nil))
  350.      (progn
  351.        (vc-file-setprop file 'vc-checkout-time lastmod)
  352.        t)
  353.        (progn
  354.        (vc-file-setprop file 'vc-checkout-time '(0 . 0))
  355.        nil
  356.      ))
  357.      )))
  358.  
  359. (defun vc-next-action-on-file (file verbose &optional comment)
  360.   ;;; If comment is specified, it will be used as an admin or checkin comment.
  361.   (let (owner version (vc-file (vc-name file)))
  362.     (cond
  363.  
  364.      ;; if there is no master file corresponding, create one
  365.      ((not vc-file)
  366.       (vc-register verbose comment)
  367.       (if vc-initial-comment
  368.       (setq vc-log-after-operation-hook
  369.         'vc-checkout-writable-buffer-hook)
  370.     (vc-checkout-writable-buffer file)))
  371.  
  372.      ;; if there is no lock on the file, assert one and get it
  373.      ((not (setq owner (vc-locking-user file)))
  374.       (if (and vc-checkout-carefully
  375.            (not (vc-workfile-unchanged-p file))
  376.            (not (zerop (vc-backend-diff file nil))))
  377.       (if (save-window-excursion
  378.         (pop-to-buffer "*vc*")
  379.         (goto-char (point-min))
  380.         (insert-string (format "Changes to %s since last lock:\n\n"
  381.                        file))
  382.         (not (beep))
  383.         (yes-or-no-p
  384.               (concat "File has unlocked changes, "
  385.                "claim lock retaining changes? ")))
  386.           (progn (vc-backend-steal file)
  387.              (vc-mode-line file))
  388.         (if (not (yes-or-no-p "Revert to checked-in version, instead? "))
  389.         (error "Checkout aborted.")
  390.           (vc-revert-buffer1 t t)
  391.           (vc-checkout-writable-buffer file))
  392.         )
  393.     (vc-checkout-writable-buffer file)))
  394.  
  395.      ;; a checked-out version exists, but the user may not own the lock
  396.      ((not (string-equal owner (user-login-name)))
  397.       (if comment
  398.       (error "Sorry, you can't steal the lock on %s this way" file))
  399.       (vc-steal-lock
  400.        file
  401.        (and verbose (read-string "Version to steal: "))
  402.        owner))
  403.      
  404.      ;; OK, user owns the lock on the file
  405.      (t
  406.       (find-file file)
  407.  
  408.       ;; give luser a chance to save before checking in.
  409.       (vc-buffer-sync)
  410.  
  411.       ;; Revert if file is unchanged and buffer is too.
  412.       ;; If buffer is modified, that means the user just said no
  413.       ;; to saving it; in that case, don't revert,
  414.       ;; because the user might intend to save
  415.       ;; after finishing the log entry.
  416.       (if (and (vc-workfile-unchanged-p file)
  417.            (not (buffer-modified-p)))
  418.           (progn
  419.         (vc-backend-revert file)
  420.         ;; DO NOT revert the file without asking the user!
  421.         (vc-resynch-window file t nil))
  422.  
  423.         ;; user may want to set nonstandard parameters
  424.         (if verbose
  425.         (setq version (read-string "New version level: ")))
  426.  
  427.         ;; OK, let's do the checkin
  428.         (vc-checkin file version comment)
  429.         )))))
  430.  
  431. (defun vc-next-action-dired (file rev comment)
  432.   ;; We've accepted a log comment, now do a vc-next-action using it on all
  433.   ;; marked files.
  434.   (set-buffer vc-parent-buffer)
  435.   (dired-map-over-marks
  436.    (save-window-excursion
  437.      (let ((file (dired-get-filename)))
  438.        (message "Processing %s..." file)
  439.        (vc-next-action-on-file file nil comment)
  440.        (message "Processing %s...done" file)))
  441.    nil t)
  442.   )
  443.  
  444. ;; Here's the major entry point.
  445.  
  446. ;;;###autoload
  447. (defun vc-next-action (verbose)
  448.   "Do the next logical checkin or checkout operation on the current file.
  449.    If the file is not already registered, this registers it for version
  450. control and then retrieves a writable, locked copy for editing.
  451.    If the file is registered and not locked by anyone, this checks out
  452. a writable and locked file ready for editing.
  453.    If the file is checked out and locked by the calling user, this
  454. first checks to see if the file has changed since checkout.  If not,
  455. it performs a revert.
  456.    If the file has been changed, this pops up a buffer for entry
  457. of a log message; when the message has been entered, it checks in the
  458. resulting changes along with the log message as change commentary.  If
  459. the variable `vc-keep-workfiles' is non-nil (which is its default), a
  460. read-only copy of the changed file is left in place afterwards.
  461.    If the file is registered and locked by someone else, you are given
  462. the option to steal the lock.
  463.    If you call this from within a VC dired buffer with no files marked,
  464. it will operate on the file in the current line.
  465.    If you call this from within a VC dired buffer, and one or more
  466. files are marked, it will accept a log message and then operate on
  467. each one.  The log message will be used as a comment for any register
  468. or checkin operations, but ignored when doing checkouts.  Attempted
  469. lock steals will raise an error.
  470.  
  471.    For checkin, a prefix argument lets you specify the version number to use."
  472.   (interactive "P")
  473.   (catch 'nogo
  474.     (if vc-dired-mode
  475.     (let ((files (dired-get-marked-files)))
  476.       (if (= (length files) 1)
  477.           (find-file-other-window (dired-get-filename))
  478.         (vc-start-entry nil nil nil
  479.                 "Enter a change comment for the marked files."
  480.                 'vc-next-action-dired)
  481.         (throw 'nogo nil))))
  482.     (while vc-parent-buffer
  483.       (pop-to-buffer vc-parent-buffer))
  484.     (if buffer-file-name
  485.     (vc-next-action-on-file buffer-file-name verbose)
  486.       (vc-registration-error nil))))
  487.  
  488. ;;; These functions help the vc-next-action entry point
  489.  
  490. (defun vc-checkout-writable-buffer (&optional file)
  491.   "Retrieve a writable copy of the latest version of the current buffer's file."
  492.   (vc-checkout (or file (buffer-file-name)) t)
  493.   )
  494.  
  495. ;;;###autoload
  496. (defun vc-register (&optional override comment)
  497.   "Register the current file into your version-control system."
  498.   (interactive "P")
  499.   (if (vc-name buffer-file-name)
  500.       (error "This file is already registered"))
  501.   ;; Watch out for new buffers of size 0: the corresponding file
  502.   ;; does not exist yet, even though buffer-modified-p is nil.
  503.   (if (and (not (buffer-modified-p))
  504.        (zerop (buffer-size))
  505.        (not (file-exists-p buffer-file-name)))
  506.       (set-buffer-modified-p t))
  507.   (vc-buffer-sync)
  508.   (vc-admin
  509.    buffer-file-name
  510.    (and override
  511.     (read-string
  512.      (format "Initial version level for %s: " buffer-file-name))))
  513.   )
  514.  
  515. (defun vc-resynch-window (file &optional keep noquery)
  516.   ;; If the given file is in the current buffer,
  517.   ;; either revert on it so we see expanded keyworks,
  518.   ;; or unvisit it (depending on vc-keep-workfiles)
  519.   ;; NOQUERY if non-nil inhibits confirmation for reverting.
  520.   ;; NOQUERY should be t *only* if it is known the only difference
  521.   ;; between the buffer and the file is due to RCS rather than user editing!
  522.   (and (string= buffer-file-name file)
  523.        (if keep
  524.        (progn
  525.          (vc-revert-buffer1 t noquery)
  526.          (vc-mode-line buffer-file-name))
  527.      (progn
  528.        (delete-window)
  529.        (kill-buffer (current-buffer))))))
  530.  
  531. (defun vc-start-entry (file rev comment msg action)
  532.   ;; Accept a comment for an operation on FILE revision REV.  If COMMENT
  533.   ;; is nil, pop up a VC-log buffer, emit MSG, and set the
  534.   ;; action on close to ACTION; otherwise, do action immediately.
  535.   ;; Remember the file's buffer in parent-buffer (current one if no file).
  536.   (let ((parent (if file (find-file-noselect file) (current-buffer))))
  537.     (if comment
  538.     (set-buffer (get-buffer-create "*VC-log*"))
  539.       (pop-to-buffer (get-buffer-create "*VC-log*")))
  540.     (set (make-local-variable 'vc-parent-buffer) parent)
  541.     (set (make-local-variable 'vc-parent-buffer-name)
  542.      (concat " from " (buffer-name vc-parent-buffer)))
  543.     (vc-mode-line (or file " (no file)"))
  544.     (vc-log-mode)
  545.     (setq vc-log-operation action)
  546.     (setq vc-log-file file)
  547.     (setq vc-log-version rev)
  548.     (if comment
  549.     (progn
  550.       (erase-buffer)
  551.       (if (eq comment t)
  552.           (vc-finish-logentry t)
  553.         (insert comment)
  554.         (vc-finish-logentry nil)))
  555.       (message "%s  Type C-c C-c when done." msg))))
  556.  
  557. (defun vc-admin (file rev &optional comment)
  558.   "Check a file into your version-control system.
  559. FILE is the unmodified name of the file.  REV should be the base version
  560. level to check it in under.  COMMENT, if specified, is the checkin comment."
  561.       (vc-start-entry file rev
  562.               (or comment (not vc-initial-comment))
  563.               "Enter initial comment." 'vc-backend-admin))
  564.  
  565. (defun vc-checkout (file &optional writable)
  566.   "Retrieve a copy of the latest version of the given file."
  567.   ;; If ftp is on this system and the name matches the ange-ftp format
  568.   ;; for a remote file, the user is trying something that won't work.
  569.   (if (and (string-match "^/[^/:]+:" file) (vc-find-binary "ftp"))
  570.       (error "Sorry, you can't check out files over FTP"))
  571.   (vc-backend-checkout file writable)
  572.   (if (string-equal file buffer-file-name)
  573.       (vc-resynch-window file t t))
  574.   )
  575.  
  576. (defun vc-steal-lock (file rev &optional owner)
  577.   "Steal the lock on the current workfile."
  578.   (interactive)
  579.   (if (not owner)
  580.       (setq owner (vc-locking-user file)))
  581.   (if (not (y-or-n-p (format "Take the lock on %s:%s from %s? " file rev owner)))
  582.       (error "Steal cancelled"))
  583.   (pop-to-buffer (get-buffer-create "*VC-mail*"))
  584.   (setq default-directory (expand-file-name "~/"))
  585.   (auto-save-mode auto-save-default)
  586.   (mail-mode)
  587.   (erase-buffer)
  588.   (mail-setup owner (format "%s:%s" file rev) nil nil nil
  589.           (list (list 'vc-finish-steal file rev)))
  590.   (goto-char (point-max))
  591.   (insert
  592.    (format "I stole the lock on %s:%s, " file rev)
  593.    (current-time-string)
  594.    ".\n")
  595.   (message "Please explain why you stole the lock.  Type C-c C-c when done."))
  596.  
  597. ;; This is called when the notification has been sent.
  598. (defun vc-finish-steal (file version)
  599.   (vc-backend-steal file version)
  600.   (vc-resynch-window file t t))
  601.  
  602. (defun vc-checkin (file &optional rev comment)
  603.   "Check in the file specified by FILE.
  604. The optional argument REV may be a string specifying the new version level
  605. \(if nil increment the current level).  The file is either retained with write
  606. permissions zeroed, or deleted (according to the value of `vc-keep-workfiles').
  607. COMMENT is a comment string; if omitted, a buffer is
  608. popped up to accept a comment."
  609.   (setq vc-log-after-operation-hook 'vc-checkin-hook)
  610.   (vc-start-entry file rev comment "Enter a change comment." 'vc-backend-checkin))
  611.  
  612. ;;; Here is a checkin hook that may prove useful to sites using the
  613. ;;; ChangeLog facility supported by Emacs.
  614. (defun vc-comment-to-change-log (&optional whoami file-name)
  615.   "Enter last VC comment into change log file for current buffer's file.
  616. Optional arg (interactive prefix) non-nil means prompt for user name and site.
  617. Second arg is file name of change log.  \
  618. If nil, uses `change-log-default-name'."
  619.   (interactive (if current-prefix-arg
  620.            (list current-prefix-arg
  621.              (prompt-for-change-log-name))))
  622.   (let (;; Extract the comment first so we get any error before doing anything.
  623.     (comment (ring-ref vc-comment-ring 0))
  624.     ;; Don't let add-change-log-entry insert a defun name.
  625.     (add-log-current-defun-function 'ignore)
  626.     end)
  627.     ;; Call add-log to do half the work.
  628.     (add-change-log-entry whoami file-name t t)
  629.     ;; Insert the VC comment, leaving point before it.
  630.     (setq end (save-excursion (insert comment) (point-marker)))
  631.     (if (looking-at "\\s *\\s(")
  632.     ;; It starts with an open-paren, as in "(foo): Frobbed."
  633.     ;; So remove the ": " add-log inserted.
  634.     (delete-char -2))
  635.     ;; Canonicalize the white space between the file name and comment.
  636.     (just-one-space)
  637.     ;; Indent rest of the text the same way add-log indented the first line.
  638.     (let ((indentation (current-indentation)))
  639.       (save-excursion
  640.     (while (< (point) end)
  641.       (forward-line 1)
  642.       (indent-to indentation))
  643.     (setq end (point))))
  644.     ;; Fill the inserted text, preserving open-parens at bol.
  645.     (let ((paragraph-separate (concat paragraph-separate "\\|^\\s *\\s("))
  646.       (paragraph-start (concat paragraph-start "\\|^\\s *\\s(")))
  647.       (beginning-of-line)
  648.       (fill-region (point) end))
  649.     ;; Canonicalize the white space at the end of the entry so it is
  650.     ;; separated from the next entry by a single blank line.
  651.     (skip-syntax-forward " " end)
  652.     (delete-char (- (skip-syntax-backward " ")))
  653.     (or (eobp) (looking-at "\n\n")
  654.     (insert "\n"))))
  655.  
  656.  
  657. (defun vc-finish-logentry (&optional nocomment)
  658.   "Complete the operation implied by the current log entry."
  659.   (interactive)
  660.   ;; Check and record the comment, if any.
  661.   (if (not nocomment)
  662.       (progn
  663.     (goto-char (point-max))
  664.     (if (not (bolp))
  665.         (newline))
  666.     ;; Comment too long?
  667.     (vc-backend-logentry-check vc-log-file)
  668.     ;; Record the comment in the comment ring
  669.     (if (null vc-comment-ring)
  670.         (setq vc-comment-ring (make-ring vc-maximum-comment-ring-size)))
  671.     (ring-insert vc-comment-ring (buffer-string))
  672.     ))
  673.   ;; OK, do it to it
  674.   (if vc-log-operation
  675.       (save-excursion
  676.     (funcall vc-log-operation 
  677.          vc-log-file
  678.          vc-log-version
  679.          (buffer-string)))
  680.     (error "No log operation is pending"))
  681.   ;; Return to "parent" buffer of this checkin and remove checkin window
  682.   (pop-to-buffer vc-parent-buffer)
  683.   (let ((logbuf (get-buffer "*VC-log*")))
  684.     (delete-windows-on logbuf)
  685.     (kill-buffer logbuf))
  686.   ;; Now make sure we see the expanded headers
  687.   (if buffer-file-name
  688.     (vc-resynch-window buffer-file-name vc-keep-workfiles t))
  689.   (run-hooks vc-log-after-operation-hook))
  690.  
  691. ;; Code for access to the comment ring
  692.  
  693. (defun vc-previous-comment (arg)
  694.   "Cycle backwards through comment history."
  695.   (interactive "*p")
  696.   (let ((len (ring-length vc-comment-ring)))
  697.     (cond ((<= len 0)
  698.        (message "Empty comment ring")
  699.        (ding))
  700.       (t
  701.        (erase-buffer)
  702.        ;; Initialize the index on the first use of this command
  703.        ;; so that the first M-p gets index 0, and the first M-n gets
  704.        ;; index -1.
  705.        (if (null vc-comment-ring-index)
  706.            (setq vc-comment-ring-index
  707.              (if (> arg 0) -1
  708.              (if (< arg 0) 1 0))))
  709.        (setq vc-comment-ring-index
  710.          (mod (+ vc-comment-ring-index arg) len))
  711.        (message "%d" (1+ vc-comment-ring-index))
  712.        (insert (ring-ref vc-comment-ring vc-comment-ring-index))))))
  713.  
  714. (defun vc-next-comment (arg)
  715.   "Cycle forwards through comment history."
  716.   (interactive "*p")
  717.   (vc-previous-comment (- arg)))
  718.  
  719. (defun vc-comment-search-reverse (str)
  720.   "Searches backwards through comment history for substring match."
  721.   (interactive "sComment substring: ")
  722.   (if (string= str "")
  723.       (setq str vc-last-comment-match)
  724.     (setq vc-last-comment-match str))
  725.   (if (null vc-comment-ring-index)
  726.       (setq vc-comment-ring-index -1))
  727.   (let ((str (regexp-quote str))
  728.         (len (ring-length vc-comment-ring))
  729.     (n (1+ vc-comment-ring-index)))
  730.     (while (and (< n len) (not (string-match str (ring-ref vc-comment-ring n))))
  731.       (setq n (+ n 1)))
  732.     (cond ((< n len)
  733.        (vc-previous-comment (- n vc-comment-ring-index)))
  734.       (t (error "Not found")))))
  735.  
  736. (defun vc-comment-search-forward (str)
  737.   "Searches forwards through comment history for substring match."
  738.   (interactive "sComment substring: ")
  739.   (if (string= str "")
  740.       (setq str vc-last-comment-match)
  741.     (setq vc-last-comment-match str))
  742.   (if (null vc-comment-ring-index)
  743.       (setq vc-comment-ring-index 0))
  744.   (let ((str (regexp-quote str))
  745.         (len (ring-length vc-comment-ring))
  746.     (n vc-comment-ring-index))
  747.     (while (and (>= n 0) (not (string-match str (ring-ref vc-comment-ring n))))
  748.       (setq n (- n 1)))
  749.     (cond ((>= n 0)
  750.        (vc-next-comment (- n vc-comment-ring-index)))
  751.       (t (error "Not found")))))
  752.  
  753. ;; Additional entry points for examining version histories
  754.  
  755. ;;;###autoload
  756. (defun vc-diff (historic)
  757.   "Display diffs between file versions.
  758. Normally this compares the current file and buffer with the most recent 
  759. checked in version of that file.  This uses no arguments.
  760. With a prefix argument, it reads the file name to use
  761. and two version designators specifying which versions to compare."
  762.   (interactive "P")
  763.   (if vc-dired-mode
  764.       (set-buffer (find-file-noselect (dired-get-filename))))
  765.   (while vc-parent-buffer
  766.       (pop-to-buffer vc-parent-buffer))
  767.   (if historic
  768.       (call-interactively 'vc-version-diff)
  769.     (if (or (null buffer-file-name) (null (vc-name buffer-file-name)))
  770.     (error
  771.      "There is no version-control master associated with this buffer"))
  772.     (let ((file buffer-file-name)
  773.       unchanged)
  774.       (or (and file (vc-name file))
  775.       (vc-registration-error file))
  776.       (vc-buffer-sync)
  777.       (setq unchanged (vc-workfile-unchanged-p buffer-file-name))
  778.       (if unchanged
  779.       (message "No changes to %s since latest version." file)
  780.     (vc-backend-diff file nil)
  781.     ;; Ideally, we'd like at this point to parse the diff so that
  782.     ;; the buffer effectively goes into compilation mode and we
  783.     ;; can visit the old and new change locations via next-error.
  784.     ;; Unfortunately, this is just too painful to do.  The basic
  785.     ;; problem is that the `old' file doesn't exist to be
  786.     ;; visited.  This plays hell with numerous assumptions in
  787.     ;; the diff.el and compile.el machinery.
  788.     (pop-to-buffer "*vc*")
  789.     (pop-to-buffer "*vc*")
  790.     (if (= 0 (buffer-size))
  791.         (progn
  792.           (setq unchanged t)
  793.           (message "No changes to %s since latest version." file))
  794.       (goto-char (point-min))
  795.       (shrink-window-if-larger-than-buffer)))
  796.       (not unchanged))))
  797.  
  798. (defun vc-version-diff (file rel1 rel2)
  799.   "For FILE, report diffs between two stored versions REL1 and REL2 of it.
  800. If FILE is a directory, generate diffs between versions for all registered
  801. files in or below it."
  802.   (interactive "FFile or directory to diff: \nsOlder version: \nsNewer version: ")
  803.   (if (string-equal rel1 "") (setq rel1 nil))
  804.   (if (string-equal rel2 "") (setq rel2 nil))
  805.   (if (file-directory-p file)
  806.       (let ((camefrom (current-buffer)))
  807.     (set-buffer (get-buffer-create "*vc-status*"))
  808.     (set (make-local-variable 'vc-parent-buffer) camefrom)
  809.     (set (make-local-variable 'vc-parent-buffer-name)
  810.          (concat " from " (buffer-name camefrom)))
  811.     (erase-buffer)
  812.     (insert "Diffs between "
  813.         (or rel1 "last version checked in")
  814.         " and "
  815.         (or rel2 "current workfile(s)")
  816.         ":\n\n")
  817.     (set-buffer (get-buffer-create "*vc*"))
  818.     (cd file)
  819.     (vc-file-tree-walk
  820.      (function (lambda (f)
  821.              (message "Looking at %s" f)
  822.              (and
  823.               (not (file-directory-p f))
  824.               (vc-registered f)
  825.               (vc-backend-diff f rel1 rel2)
  826.               (append-to-buffer "*vc-status*" (point-min) (point-max)))
  827.              )))
  828.     (pop-to-buffer "*vc-status*")
  829.     (insert "\nEnd of diffs.\n")
  830.     (goto-char (point-min))
  831.     (set-buffer-modified-p nil)
  832.     )
  833.     (if (zerop (vc-backend-diff file rel1 rel2))
  834.     (message "No changes to %s between %s and %s." file rel1 rel2)
  835.       (pop-to-buffer "*vc*"))))
  836.  
  837. ;;;###autoload
  838. (defun vc-version-other-window (rev)
  839.   "Visit version REV of the current buffer in another window.
  840. If the current buffer is named `F', the version is named `F.~REV~'.
  841. If `F.~REV~' already exists, it is used instead of being re-created."
  842.   (interactive "sVersion to visit (default is latest version): ")
  843.   (if vc-dired-mode
  844.       (set-buffer (find-file-noselect (dired-get-filename))))
  845.   (while vc-parent-buffer
  846.       (pop-to-buffer vc-parent-buffer))
  847.   (if (and buffer-file-name (vc-name buffer-file-name))
  848.       (let* ((version (if (string-equal rev "")
  849.               (vc-latest-version buffer-file-name)
  850.             rev))
  851.          (filename (concat buffer-file-name ".~" version "~")))
  852.      (or (file-exists-p filename)
  853.          (vc-backend-checkout buffer-file-name nil version filename))
  854.      (find-file-other-window filename))
  855.     (vc-registration-error buffer-file-name)))
  856.  
  857. ;; Header-insertion code
  858.  
  859. ;;;###autoload
  860. (defun vc-insert-headers ()
  861.   "Insert headers in a file for use with your version-control system.
  862. Headers desired are inserted at the start of the buffer, and are pulled from
  863. the variable `vc-header-alist'."
  864.   (interactive)
  865.   (if vc-dired-mode
  866.       (find-file-other-window (dired-get-filename)))
  867.   (while vc-parent-buffer
  868.       (pop-to-buffer vc-parent-buffer))
  869.   (save-excursion
  870.     (save-restriction
  871.       (widen)
  872.       (if (or (not (vc-check-headers))
  873.           (y-or-n-p "Version headers already exist.  Insert another set? "))
  874.       (progn
  875.         (let* ((delims (cdr (assq major-mode vc-comment-alist)))
  876.            (comment-start-vc (or (car delims) comment-start "#"))
  877.            (comment-end-vc (or (car (cdr delims)) comment-end ""))
  878.            (hdstrings (cdr (assoc (vc-backend-deduce (buffer-file-name)) vc-header-alist))))
  879.           (mapcar (function (lambda (s)
  880.                   (insert comment-start-vc "\t" s "\t"
  881.                       comment-end-vc "\n")))
  882.               hdstrings)
  883.           (if vc-static-header-alist
  884.           (mapcar (function (lambda (f)
  885.                       (if (string-match (car f) buffer-file-name)
  886.                       (insert (format (cdr f) (car hdstrings))))))
  887.               vc-static-header-alist))
  888.           )
  889.         )))))
  890.  
  891. ;; The VC directory submode.  Coopt Dired for this.
  892. ;; All VC commands get mapped into logical equivalents.
  893.  
  894. (defvar vc-dired-prefix-map (make-sparse-keymap))
  895. (define-key vc-dired-prefix-map "\C-xv" vc-prefix-map)
  896.  
  897. (or (not (boundp 'minor-mode-map-alist))
  898.     (assq 'vc-dired-mode minor-mode-map-alist)
  899.     (setq minor-mode-map-alist
  900.        (cons (cons 'vc-dired-mode vc-dired-prefix-map)
  901.          minor-mode-map-alist)))
  902.  
  903. (defun vc-dired-mode ()
  904.   "The augmented Dired minor mode used in VC directory buffers.
  905. All Dired commands operate normally.  Users currently locking listed files
  906. are listed in place of the file's owner and group.
  907. Keystrokes bound to VC commands will execute as though they had been called
  908. on a buffer attached to the file named in the current Dired buffer line."
  909.   (setq vc-dired-mode t)
  910.   (setq vc-mode " under VC"))
  911.  
  912. (defun vc-dired-reformat-line (x)
  913.   ;; Hack a directory-listing line, plugging in locking-user info in
  914.   ;; place of the user and group info.  Should have the beneficial
  915.   ;; side-effect of shortening the listing line.  Each call starts with
  916.   ;; point immediately following the dired mark area on the line to be
  917.   ;; hacked.
  918.   ;;
  919.   ;; Simplest possible one:
  920.   ;; (insert (concat x "\t")))
  921.   ;;
  922.   ;; This code, like dired, assumes UNIX -l format.
  923.   (forward-word 1)    ;; skip over any extra field due to -ibs options
  924.   (if x (setq x (concat "(" x ")")))
  925.   (if (re-search-forward "\\([0-9]+ \\).................\\( .*\\)" nil 0)
  926.       (let ((rep (substring (concat x "                 ") 0 9)))
  927.     (replace-match (concat "\\1" rep "\\2") t)))
  928.   )
  929.  
  930. ;;; Note in Emacs 18 the following defun gets overridden
  931. ;;; with the symbol 'vc-directory-18.  See below.
  932. ;;;###autoload
  933. (defun vc-directory (verbose)
  934.   "Show version-control status of all files under the current directory."
  935.   (interactive "P")
  936.   (let (nonempty
  937.     (dl (length default-directory))
  938.     (filelist nil) (userlist nil)
  939.     dired-buf
  940.     dired-buf-mod-count)
  941.     (vc-file-tree-walk
  942.      (function (lambda (f)
  943.          (if (vc-registered f)
  944.              (let ((user (vc-locking-user f)))
  945.                (and (or verbose user)
  946.                 (setq filelist (cons (substring f dl) filelist))
  947.                 (setq userlist (cons user userlist))))))))
  948.     (save-excursion
  949.       ;; This uses a semi-documented feature of dired; giving a switch
  950.       ;; argument forces the buffer to refresh each time.
  951.       (dired
  952.        (cons default-directory (nreverse filelist))
  953.        dired-listing-switches)
  954.       (setq dired-buf (current-buffer))
  955.       (setq nonempty (not (zerop (buffer-size)))))
  956.     (if nonempty
  957.     (progn
  958.       (pop-to-buffer dired-buf)
  959.       (vc-dired-mode)
  960.       (goto-char (point-min))
  961.       (setq buffer-read-only nil)
  962.       (forward-line 1)    ;; Skip header line
  963.       (mapcar
  964.        (function
  965.         (lambda (x)
  966.          (forward-char 2)    ;; skip dired's mark area
  967.          (vc-dired-reformat-line x)
  968.          (forward-line 1)))    ;; go to next line
  969.        (nreverse userlist))
  970.       (setq buffer-read-only t)
  971.       (goto-char (point-min))
  972.       )
  973.       (message "No files are currently %s under %s"
  974.            (if verbose "registered" "locked") default-directory))
  975.     ))
  976.  
  977. ;; Emacs 18 version
  978. (defun vc-directory-18 (verbose)
  979.   "Show version-control status of all files under the current directory."
  980.   (interactive "P")
  981.   (let (nonempty (dir default-directory))
  982.     (save-excursion
  983.       (set-buffer (get-buffer-create "*vc-status*"))
  984.       (erase-buffer)
  985.       (cd dir)
  986.       (vc-file-tree-walk
  987.        (function (lambda (f)
  988.            (if (vc-registered f)
  989.                (let ((user (vc-locking-user f)))
  990.              (if (or user verbose)
  991.                  (insert (format
  992.                       "%s    %s\n"
  993.                       (concat user) f))))))))
  994.       (setq nonempty (not (zerop (buffer-size)))))
  995.     (if nonempty
  996.     (progn
  997.       (pop-to-buffer "*vc-status*" t)
  998.       (goto-char (point-min))
  999.       (shrink-window-if-larger-than-buffer)))
  1000.       (message "No files are currently %s under %s"
  1001.            (if verbose "registered" "locked") default-directory))
  1002.     )
  1003.  
  1004. (or (boundp 'minor-mode-map-alist)
  1005.     (fset 'vc-directory 'vc-directory-18))
  1006.  
  1007. ; Emacs 18 also lacks these.
  1008. (or (boundp 'compilation-old-error-list)
  1009.     (setq compilation-old-error-list nil))
  1010.  
  1011. ;; Named-configuration support for SCCS
  1012.  
  1013. (defun vc-add-triple (name file rev)
  1014.   (save-excursion
  1015.     (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
  1016.     (goto-char (point-max))
  1017.     (insert name "\t:\t" file "\t" rev "\n")
  1018.     (basic-save-buffer)
  1019.     (kill-buffer (current-buffer))
  1020.     ))
  1021.  
  1022. (defun vc-record-rename (file newname)
  1023.   (save-excursion
  1024.     (find-file (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file))
  1025.     (goto-char (point-min))
  1026.     (replace-regexp (concat ":" (regexp-quote file) "$") (concat ":" newname))
  1027.     (basic-save-buffer)
  1028.     (kill-buffer (current-buffer))
  1029.     ))
  1030.  
  1031. (defun vc-lookup-triple (file name)
  1032.   ;; Return the numeric version corresponding to a named snapshot of file
  1033.   ;; If name is nil or a version number string it's just passed through
  1034.   (cond ((null name) "")
  1035.     ((let ((firstchar (aref name 0)))
  1036.        (and (>= firstchar ?0) (<= firstchar ?9)))
  1037.      name)
  1038.     (t
  1039.      (car (vc-master-info
  1040.            (concat (vc-backend-subdirectory-name file) "/" vc-name-assoc-file)
  1041.            (list (concat name "\t:\t" file "\t\\(.+\\)"))))
  1042.      )))
  1043.  
  1044. ;; Named-configuration entry points
  1045.  
  1046. (defun vc-locked-example ()
  1047.   ;; Return an example of why the current directory is not ready to be snapshot
  1048.   ;; or nil if no such example exists.
  1049.   (catch 'vc-locked-example
  1050.     (vc-file-tree-walk
  1051.      (function (lambda (f)
  1052.          (if (and (vc-registered f) (vc-locking-user f))
  1053.              (throw 'vc-locked-example f)))))
  1054.     nil))
  1055.  
  1056. ;;;###autoload
  1057. (defun vc-create-snapshot (name)
  1058.   "Make a snapshot called NAME.
  1059. The snapshot is made from all registered files at or below the current
  1060. directory.  For each file, the version level of its latest
  1061. version becomes part of the named configuration."
  1062.   (interactive "sNew snapshot name: ")
  1063.   (let ((locked (vc-locked-example)))
  1064.     (if locked
  1065.     (error "File %s is locked" locked)
  1066.       (vc-file-tree-walk
  1067.        (function (lambda (f) (and
  1068.                   (vc-name f)
  1069.                   (vc-backend-assign-name f name)))))
  1070.       )))
  1071.  
  1072. ;;;###autoload
  1073. (defun vc-retrieve-snapshot (name)
  1074.   "Retrieve the snapshot called NAME.
  1075. This function fails if any files are locked at or below the current directory
  1076. Otherwise, all registered files are checked out (unlocked) at their version
  1077. levels in the snapshot."
  1078.   (interactive "sSnapshot name to retrieve: ")
  1079.   (let ((locked (vc-locked-example)))
  1080.     (if locked
  1081.     (error "File %s is locked" locked)
  1082.       (vc-file-tree-walk
  1083.        (function (lambda (f) (and
  1084.                   (vc-name f)
  1085.                   (vc-error-occurred
  1086.                    (vc-backend-checkout f nil name))))))
  1087.       )))
  1088.  
  1089. ;; Miscellaneous other entry points
  1090.  
  1091. ;;;###autoload
  1092. (defun vc-print-log ()
  1093.   "List the change log of the current buffer in a window."
  1094.   (interactive)
  1095.   (if vc-dired-mode
  1096.       (set-buffer (find-file-noselect (dired-get-filename))))
  1097.   (while vc-parent-buffer
  1098.       (pop-to-buffer vc-parent-buffer))
  1099.   (if (and buffer-file-name (vc-name buffer-file-name))
  1100.       (progn
  1101.     (vc-backend-print-log buffer-file-name)
  1102.     (pop-to-buffer (get-buffer-create "*vc*"))
  1103.     (while (looking-at "=*\n")
  1104.       (delete-char (- (match-end 0) (match-beginning 0)))
  1105.       (forward-line -1))
  1106.     (goto-char (point-min))
  1107.     (if (looking-at "[\b\t\n\v\f\r ]+")
  1108.         (delete-char (- (match-end 0) (match-beginning 0))))
  1109.     (shrink-window-if-larger-than-buffer)
  1110.     )
  1111.     (vc-registration-error buffer-file-name)
  1112.     )
  1113.   )
  1114.  
  1115. ;;;###autoload
  1116. (defun vc-revert-buffer ()
  1117.   "Revert the current buffer's file back to the latest checked-in version.
  1118. This asks for confirmation if the buffer contents are not identical
  1119. to that version."
  1120.   (interactive)
  1121.   (if vc-dired-mode
  1122.       (find-file-other-window (dired-get-filename)))
  1123.   (while vc-parent-buffer
  1124.       (pop-to-buffer vc-parent-buffer))
  1125.   (let ((file buffer-file-name)
  1126.     (obuf (current-buffer)) (changed (vc-diff nil)))
  1127.     (if (and changed (or vc-suppress-confirm
  1128.              (not (yes-or-no-p "Discard changes? "))))
  1129.     (progn
  1130.       (delete-window)
  1131.       (error "Revert cancelled"))
  1132.       (set-buffer obuf))
  1133.     (if changed
  1134.     (delete-window))
  1135.     (vc-backend-revert file)
  1136.     (vc-resynch-window file t t)
  1137.     )
  1138.   )
  1139.  
  1140. ;;;###autoload
  1141. (defun vc-cancel-version (norevert)
  1142.   "Get rid of most recently checked in version of this file.
  1143. A prefix argument means do not revert the buffer afterwards."
  1144.   (interactive "P")
  1145.   (if vc-dired-mode
  1146.       (find-file-other-window (dired-get-filename)))
  1147.   (while vc-parent-buffer
  1148.     (pop-to-buffer vc-parent-buffer))
  1149.   (let* ((target (concat (vc-latest-version (buffer-file-name))))
  1150.     (yours (concat (vc-your-latest-version (buffer-file-name))))
  1151.     (prompt (if (string-equal yours target)
  1152.             "Remove your version %s from master? "
  1153.           "Version %s was not your change.  Remove it anyway? ")))
  1154.     (if (null (yes-or-no-p (format prompt target)))
  1155.     nil
  1156.       (vc-backend-uncheck (buffer-file-name) target)
  1157.       (if (or norevert
  1158.           (not (yes-or-no-p "Revert buffer to most recent remaining version? ")))
  1159.       (vc-mode-line (buffer-file-name))
  1160.     (vc-checkout (buffer-file-name) nil)))
  1161.     ))
  1162.  
  1163. (defun vc-rename-file (old new)
  1164.   "Rename file OLD to NEW, and rename its master file likewise."
  1165.   (interactive "fVC rename file: \nFRename to: ")
  1166.   (let ((oldbuf (get-file-buffer old)))
  1167.     (if (and oldbuf (buffer-modified-p oldbuf))
  1168.     (error "Please save files before moving them"))
  1169.     (if (get-file-buffer new)
  1170.     (error "Already editing new file name"))
  1171.     (if (file-exists-p new)
  1172.     (error "New file already exists"))
  1173.     (let ((oldmaster (vc-name old)))
  1174.       (if oldmaster
  1175.       (progn
  1176.         (if (vc-locking-user old)
  1177.         (error "Please check in files before moving them"))
  1178.         (if (or (file-symlink-p oldmaster)
  1179.             ;; This had FILE, I changed it to OLD. -- rms.
  1180.             (file-symlink-p (vc-backend-subdirectory-name old)))
  1181.         (error "This is not a safe thing to do in the presence of symbolic links"))
  1182.         (rename-file
  1183.          oldmaster
  1184.          (let ((backend (vc-backend-deduce old))
  1185.            (newdir (or (file-name-directory new) ""))
  1186.            (newbase (file-name-nondirectory new)))
  1187.            (catch 'found
  1188.          (mapcar
  1189.           (function
  1190.            (lambda (s)
  1191.              (if (eq backend (cdr s))
  1192.              (let* ((newmaster (format (car s) newdir newbase))
  1193.                 (newmasterdir (file-name-directory newmaster)))
  1194.                (if (or (not newmasterdir)
  1195.                    (file-directory-p newmasterdir))
  1196.                    (throw 'found newmaster))))))
  1197.           vc-master-templates)
  1198.          (error "New file lacks a version control directory"))))))
  1199.       (if (or (not oldmaster) (file-exists-p old))
  1200.       (rename-file old new)))
  1201. ; ?? Renaming a file might change its contents due to keyword expansion.
  1202. ; We should really check out a new copy if the old copy was precisely equal
  1203. ; to some checked in version.  However, testing for this is tricky....
  1204.     (if oldbuf
  1205.     (save-excursion
  1206.       (set-buffer oldbuf)
  1207.       (set-visited-file-name new)
  1208.       (set-buffer-modified-p nil))))
  1209.   ;; This had FILE, I changed it to OLD. -- rms.
  1210.   (vc-backend-dispatch old
  1211.                (vc-record-rename old new)
  1212.                nil)
  1213.   )
  1214.  
  1215. ;;;###autoload
  1216. (defun vc-update-change-log (&rest args)
  1217.   "Find change log file and add entries from recent RCS logs.
  1218. The mark is left at the end of the text prepended to the change log.
  1219. With prefix arg of C-u, only find log entries for the current buffer's file.
  1220. With any numeric prefix arg, find log entries for all files currently visited.
  1221. Otherwise, find log entries for all registered files in the default directory.
  1222. From a program, any arguments are passed to the `rcs2log' script."
  1223.   (interactive
  1224.    (cond ((consp current-prefix-arg)    ;C-u
  1225.       (list buffer-file-name))
  1226.      (current-prefix-arg        ;Numeric argument.
  1227.       (let ((files nil)
  1228.         (buffers (buffer-list))
  1229.         file)
  1230.         (while buffers
  1231.           (setq file (buffer-file-name (car buffers)))
  1232.           (and file (vc-backend-deduce file)
  1233.            (setq files (cons file files)))
  1234.           (setq buffers (cdr buffers)))
  1235.         files))
  1236.      (t
  1237.       (let ((RCS (concat default-directory "RCS")))
  1238.         (and (file-directory-p RCS)
  1239.          (mapcar (function
  1240.               (lambda (f)
  1241.                 (if (string-match "\\(.*\\),v$" f)
  1242.                 (substring f 0 (match-end 1))
  1243.                   f)))
  1244.              (directory-files RCS nil "...\\|^[^.]\\|^.[^.]")))))))
  1245.   (let ((odefault default-directory))
  1246.     (find-file-other-window (find-change-log))
  1247.     (barf-if-buffer-read-only)
  1248.     (vc-buffer-sync)
  1249.     (undo-boundary)
  1250.     (goto-char (point-min))
  1251.     (push-mark)
  1252.     (message "Computing change log entries...")
  1253.     (message "Computing change log entries... %s"
  1254.          (if (or (null args)
  1255.              (eq 0 (apply 'call-process "rcs2log" nil t nil
  1256.                   (mapcar (function
  1257.                        (lambda (f)
  1258.                          (file-relative-name
  1259.                           (if (file-name-absolute-p f)
  1260.                           f
  1261.                         (concat odefault f)))))
  1262.                       args))))
  1263.          "done" "failed"))))
  1264.  
  1265. ;; Functions for querying the master and lock files.
  1266.  
  1267. (defun vc-match-substring (bn)
  1268.   (buffer-substring (match-beginning bn) (match-end bn)))
  1269.  
  1270. (defun vc-parse-buffer (patterns &optional file properties)
  1271.   ;; Use PATTERNS to parse information out of the current buffer
  1272.   ;; by matching each regular expression in the list and returning \\1.
  1273.   ;; If a regexp has two tag brackets, assume the second is a date
  1274.   ;; field and we want the most recent entry matching the template.
  1275.   ;; If FILE and PROPERTIES are given, the latter must be a list of
  1276.   ;; properties of the same length as PATTERNS; each property is assigned 
  1277.   ;; the corresponding value.
  1278.   (mapcar (function (lambda (p)
  1279.          (goto-char (point-min))
  1280.          (if (string-match "\\\\(.*\\\\(" p)
  1281.          (let ((latest-date "") (latest-val))
  1282.            (while (re-search-forward p nil t)
  1283.              (let ((date (vc-match-substring 2)))
  1284.                (if (string< latest-date date)
  1285.                (progn
  1286.                  (setq latest-date date)
  1287.                  (setq latest-val
  1288.                    (vc-match-substring 1))))))
  1289.            latest-val))
  1290.          (prog1
  1291.          (and (re-search-forward p nil t)
  1292.               (let ((value (vc-match-substring 1)))
  1293.             (if file
  1294.                 (vc-file-setprop file (car properties) value))
  1295.             value))
  1296.            (setq properties (cdr properties)))))
  1297.       patterns)
  1298.   )
  1299.  
  1300. (defun vc-master-info (file fields &optional rfile properties)
  1301.   ;; Search for information in a master file.
  1302.   (if (and file (file-exists-p file))
  1303.       (save-excursion
  1304.     (let ((buf))
  1305.       (setq buf (create-file-buffer file))
  1306.       (set-buffer buf))
  1307.     (erase-buffer)
  1308.     (insert-file-contents file nil)
  1309.     (set-buffer-modified-p nil)
  1310.     (auto-save-mode nil)
  1311.     (prog1
  1312.         (vc-parse-buffer fields rfile properties)
  1313.       (kill-buffer (current-buffer)))
  1314.     )
  1315.     (if rfile
  1316.     (mapcar
  1317.      (function (lambda (p) (vc-file-setprop rfile p nil)))
  1318.      properties))
  1319.     )
  1320.   )
  1321.  
  1322. (defun vc-log-info (command file patterns &optional properties)
  1323.   ;; Search for information in log program output
  1324.   (if (and file (file-exists-p file))
  1325.       (save-excursion
  1326.     (let ((buf))
  1327.       (setq buf (get-buffer-create "*vc*"))
  1328.       (set-buffer buf))
  1329.     (apply 'vc-do-command 0 command file nil)
  1330.     (set-buffer-modified-p nil)
  1331.     (prog1
  1332.         (vc-parse-buffer patterns file properties)
  1333.       (kill-buffer (current-buffer))
  1334.       )
  1335.     )
  1336.     (if file
  1337.     (mapcar
  1338.      (function (lambda (p) (vc-file-setprop file p nil)))
  1339.      properties))
  1340.     )
  1341.   )
  1342.  
  1343. (defun vc-locking-user (file)
  1344.   "Return the name of the person currently holding a lock on FILE.
  1345. Return nil if there is no such person."
  1346.   (setq file (expand-file-name file))    ;; ??? Work around bug in 19.0.4
  1347.   (if (or (not vc-keep-workfiles)
  1348.       (eq vc-mistrust-permissions 't)
  1349.       (and vc-mistrust-permissions
  1350.            (funcall vc-mistrust-permissions (vc-backend-subdirectory-name file))))
  1351.       (vc-true-locking-user file)
  1352.     ;; This implementation assumes that any file which is under version
  1353.     ;; control and has -rw-r--r-- is locked by its owner.  This is true
  1354.     ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
  1355.     ;; We have to be careful not to exclude files with execute bits on;
  1356.     ;; scripts can be under version control too.  The advantage of this
  1357.     ;; hack is that calls to the very expensive vc-fetch-properties
  1358.     ;; function only have to be made if (a) the file is locked by someone
  1359.     ;; other than the current user, or (b) some untoward manipulation
  1360.     ;; behind vc's back has changed the owner or the `group' or `other'
  1361.     ;; write bits.
  1362.     (let ((attributes (file-attributes file)))
  1363.       (cond ((string-match ".r-.r-.r-." (nth 8 attributes))
  1364.          nil)
  1365.         ((and (= (nth 2 attributes) (user-uid))
  1366.           (string-match ".rw.r-.r-." (nth 8 attributes)))
  1367.          (user-login-name))
  1368.         (t
  1369.          (vc-true-locking-user file))))))
  1370.  
  1371. (defun vc-true-locking-user (file)
  1372.   ;; The slow but reliable version
  1373.   (vc-fetch-properties file)
  1374.   (vc-file-getprop file 'vc-locking-user))
  1375.  
  1376. (defun vc-latest-version (file)
  1377.   ;; Return version level of the latest version of FILE
  1378.   (vc-fetch-properties file)
  1379.   (vc-file-getprop file 'vc-latest-version))
  1380.  
  1381. (defun vc-your-latest-version (file)
  1382.   ;; Return version level of the latest version of FILE checked in by you
  1383.   (vc-fetch-properties file)
  1384.   (vc-file-getprop file 'vc-your-latest-version))
  1385.  
  1386. ;; Collect back-end-dependent stuff here
  1387. ;;
  1388. ;; Everything eventually funnels through these functions.  To implement
  1389. ;; support for a new version-control system, add another branch to the
  1390. ;; vc-backend-dispatch macro and fill it in in each call.  The variable
  1391. ;; vc-master-templates in vc-hooks.el will also have to change.
  1392.  
  1393. (defmacro vc-backend-dispatch (f s r)
  1394.   "Execute FORM1 or FORM2 depending on whether we're using SCCS or RCS."
  1395.   (list 'let (list (list 'type (list 'vc-backend-deduce f)))
  1396.     (list 'cond
  1397.           (list (list 'eq 'type (quote 'SCCS)) s)    ;; SCCS
  1398.           (list (list 'eq 'type (quote 'RCS)) r)    ;; RCS
  1399.           )))
  1400.  
  1401. (defun vc-lock-file (file)
  1402.   ;; Generate lock file name corresponding to FILE
  1403.   (let ((master (vc-name file)))
  1404.     (and
  1405.      master
  1406.      (string-match "\\(.*/\\)s\\.\\(.*\\)" master)
  1407.      (concat
  1408.       (substring master (match-beginning 1) (match-end 1))
  1409.       "p."
  1410.       (substring master (match-beginning 2) (match-end 2))))))
  1411.  
  1412.  
  1413. (defun vc-fetch-properties (file)
  1414.   ;; Re-fetch all properties associated with the given file.
  1415.   ;; Currently these properties are:
  1416.   ;;    vc-locking-user
  1417.   ;;    vc-locked-version
  1418.   ;;    vc-latest-version
  1419.   ;;    vc-your-latest-version
  1420.   (vc-backend-dispatch
  1421.    file
  1422.    ;; SCCS
  1423.    (progn
  1424.      (vc-master-info (vc-lock-file file)
  1425.              (list
  1426.               "^[^ ]+ [^ ]+ \\([^ ]+\\)"
  1427.               "^\\([^ ]+\\)")
  1428.              file
  1429.              '(vc-locking-user vc-locked-version))
  1430.      (vc-master-info (vc-name file)
  1431.           (list
  1432.            "^\001d D \\([^ ]+\\)"
  1433.            (concat "^\001d D \\([^ ]+\\) .* " 
  1434.                (regexp-quote (user-login-name)) " ")
  1435.            )
  1436.           file
  1437.           '(vc-latest-version vc-your-latest-version))
  1438.      )
  1439.    ;; RCS
  1440.    (vc-log-info "rlog" file
  1441.         (list
  1442.          "^locks: strict\n\t\\([^:]+\\)"
  1443.          "^locks: strict\n\t[^:]+: \\(.+\\)"
  1444.          "^revision[\t ]+\\([0-9.]+\\).*\ndate: \\([ /0-9:]+\\);"
  1445.          (concat
  1446.           "^revision[\t ]+\\([0-9.]+\\)\n.*author: "
  1447.           (regexp-quote (user-login-name))
  1448.           ";"))
  1449.         '(vc-locking-user vc-locked-version
  1450.                   vc-latest-version vc-your-latest-version))
  1451.    ))
  1452.  
  1453. (defun vc-backend-subdirectory-name (&optional file)
  1454.   ;; Where the master and lock files for the current directory are kept
  1455.   (symbol-name
  1456.    (or
  1457.     (and file (vc-backend-deduce file))
  1458.     vc-default-back-end
  1459.     (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))))
  1460.  
  1461. (defun vc-backend-admin (file &optional rev comment)
  1462.   ;; Register a file into the version-control system
  1463.   ;; Automatically retrieves a read-only version of the file with
  1464.   ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
  1465.   ;; it deletes the workfile.
  1466.   (vc-file-clearprops file)
  1467.   (or vc-default-back-end
  1468.       (setq vc-default-back-end (if (vc-find-binary "rcs") 'RCS 'SCCS)))
  1469.   (message "Registering %s..." file)
  1470.   (let ((backend
  1471.      (cond
  1472.       ((file-exists-p (vc-backend-subdirectory-name)) vc-default-back-end)
  1473.       ((file-exists-p "RCS") 'RCS)
  1474.       ((file-exists-p "SCCS") 'SCCS)
  1475.       (t vc-default-back-end))))
  1476.     (cond ((eq backend 'SCCS)
  1477.        (vc-do-command 0 "admin" file    ;; SCCS
  1478.               (and rev (concat "-r" rev))
  1479.               "-fb"
  1480.               (concat "-i" file)
  1481.               (and comment (concat "-y" comment))
  1482.               (format
  1483.                (car (rassq 'SCCS vc-master-templates))
  1484.                (or (file-name-directory file) "")
  1485.                (file-name-nondirectory file)))
  1486.        (delete-file file)
  1487.        (if vc-keep-workfiles
  1488.            (vc-do-command 0 "get" file)))
  1489.       ((eq backend 'RCS)
  1490.        (vc-do-command 0 "ci" file    ;; RCS
  1491.               (concat (if vc-keep-workfiles "-u" "-r") rev)
  1492.               (and comment (concat "-t-" comment))
  1493.               file)
  1494.        )))
  1495.   (message "Registering %s...done" file)
  1496.   )
  1497.  
  1498. (defun vc-backend-checkout (file &optional writable rev workfile)
  1499.   ;; Retrieve a copy of a saved version into a workfile
  1500.   (let ((filename (or workfile file)))
  1501.     (message "Checking out %s..." filename)
  1502.     (vc-backend-dispatch file
  1503.      (vc-do-command 0 "get" file    ;; SCCS
  1504.             (if writable "-e")
  1505.             (if workfile  (concat "-G" workfile))
  1506.             (and rev (concat "-r" (vc-lookup-triple file rev))))
  1507.      (if workfile ;; RCS
  1508.      ;; RCS doesn't let us check out into arbitrary file names directly.
  1509.      ;; Use `co -p' and make stdout point to the correct file.
  1510.      (let ((vc-modes (logior (file-modes (vc-name file))
  1511.                  (if writable 128 0)))
  1512.            (failed t))
  1513.        (unwind-protect
  1514.            (progn
  1515.            (vc-do-command
  1516.               0 "/bin/sh" file "-c"
  1517.               (format "umask %o; exec >\"$1\" || exit; shift; umask %o; exec co \"$@\""
  1518.                   (logand 511 (lognot vc-modes))
  1519.                   (logand 511 (lognot (default-file-modes))))
  1520.               "" ; dummy argument for shell's $0
  1521.               filename
  1522.               (if writable "-l")
  1523.               (concat "-p" rev))
  1524.            (setq failed nil))
  1525.          (and failed (file-exists-p filename) (delete-file filename))))
  1526.        (vc-do-command 0 "co" file
  1527.               (if writable "-l")
  1528.               (and rev (concat "-r" rev))))
  1529.      )
  1530.     (or workfile
  1531.     (vc-file-setprop file 'vc-checkout-time (nth 5 (file-attributes file))))
  1532.     (message "Checking out %s...done" filename))
  1533.   )
  1534.  
  1535. (defun vc-backend-logentry-check (file)
  1536.   (vc-backend-dispatch file
  1537.    (if (>= (buffer-size) 512)    ;; SCCS
  1538.        (progn
  1539.      (goto-char 512)
  1540.      (error
  1541.       "Log must be less than 512 characters; point is now at pos 512")))
  1542.    nil)
  1543.   )
  1544.  
  1545. (defun vc-backend-checkin (file &optional rev comment)
  1546.   ;; Register changes to FILE as level REV with explanatory COMMENT.
  1547.   ;; Automatically retrieves a read-only version of the file with
  1548.   ;; keywords expanded if vc-keep-workfiles is non-nil, otherwise
  1549.   ;; it deletes the workfile.
  1550.   (message "Checking in %s..." file)
  1551.   (save-excursion
  1552.     ;; Change buffers to get local value of vc-checkin-switches.
  1553.     (set-buffer (or (get-file-buffer file) (current-buffer)))
  1554.     (vc-backend-dispatch file
  1555.       (progn
  1556.     (apply 'vc-do-command 0 "delta" file
  1557.            (if rev (concat "-r" rev))
  1558.            (concat "-y" comment)
  1559.            vc-checkin-switches)
  1560.     (if vc-keep-workfiles
  1561.         (vc-do-command 0 "get" file))
  1562.     )
  1563.       (apply 'vc-do-command 0 "ci" file
  1564.          (concat (if vc-keep-workfiles "-u" "-r") rev)
  1565.          (concat "-m" comment)
  1566.          vc-checkin-switches)
  1567.       ))
  1568.   (vc-file-setprop file 'vc-locking-user nil)
  1569.   (message "Checking in %s...done" file)
  1570.   )
  1571.  
  1572. (defun vc-backend-revert (file)
  1573.   ;; Revert file to latest checked-in version.
  1574.   (message "Reverting %s..." file)
  1575.   (vc-backend-dispatch
  1576.    file
  1577.    (progn            ;; SCCS
  1578.      (vc-do-command 0 "unget" file nil)
  1579.      (vc-do-command 0 "get" file nil))
  1580.    (vc-do-command 0 "co" file "-f" "-u")) ;; RCS.  This deletes the work file.
  1581.   (vc-file-setprop file 'vc-locking-user nil)
  1582.   (message "Reverting %s...done" file)
  1583.   )
  1584.  
  1585. (defun vc-backend-steal (file &optional rev)
  1586.   ;; Steal the lock on the current workfile.  Needs RCS 5.6.2 or later for -M.
  1587.   (message "Stealing lock on %s..." file)
  1588.   (vc-backend-dispatch file
  1589.    (progn
  1590.      (vc-do-command 0 "unget" file "-n" (if rev (concat "-r" rev)))
  1591.      (vc-do-command 0 "get" file "-g" (if rev (concat "-r" rev)))
  1592.      )
  1593.    (vc-do-command 0 "rcs" file "-M" (concat "-u" rev) (concat "-l" rev)))
  1594.   (vc-file-setprop file 'vc-locking-user (user-login-name))
  1595.   (message "Stealing lock on %s...done" file)
  1596.   )  
  1597.  
  1598. (defun vc-backend-uncheck (file target)
  1599.   ;; Undo the latest checkin.  Note: this code will have to get a lot
  1600.   ;; smarter when we support multiple branches.
  1601.   (message "Removing last change from %s..." file)
  1602.   (vc-backend-dispatch file
  1603.    (vc-do-command 0 "rmdel" file (concat "-r" target))
  1604.    (vc-do-command 0 "rcs" file (concat "-o" target))
  1605.    )
  1606.   (message "Removing last change from %s...done" file)
  1607.   )
  1608.  
  1609. (defun vc-backend-print-log (file)
  1610.   ;; Print change log associated with FILE to buffer *vc*.
  1611.   (vc-do-command 0
  1612.          (vc-backend-dispatch file "prs" "rlog")
  1613.          file)
  1614.   )
  1615.  
  1616. (defun vc-backend-assign-name (file name)
  1617.   ;; Assign to a FILE's latest version a given NAME.
  1618.   (vc-backend-dispatch file
  1619.    (vc-add-triple name file (vc-latest-version file))    ;; SCCS
  1620.    (vc-do-command 0 "rcs" file (concat "-n" name ":"))    ;; RCS
  1621.    )
  1622.   )
  1623.  
  1624. (defun vc-backend-diff (file oldvers &optional newvers)
  1625.   ;; Get a difference report between two versions
  1626.   (if (eq (vc-backend-deduce file) 'SCCS)
  1627.       (setq oldvers (vc-lookup-triple file oldvers))
  1628.       (setq newvers (vc-lookup-triple file newvers)))
  1629.   (apply 'vc-do-command 1
  1630.      (or (vc-backend-dispatch file "vcdiff" "rcsdiff")
  1631.          (vc-registration-error file))
  1632.      file
  1633.      "-q"
  1634.      (and oldvers (concat "-r" oldvers))
  1635.      (and newvers (concat "-r" newvers))
  1636.      (if (listp diff-switches)
  1637.          diff-switches
  1638.        (list diff-switches))
  1639.   ))
  1640.  
  1641. (defun vc-check-headers ()
  1642.   "Check if the current file has any headers in it."
  1643.   (interactive)
  1644.   (save-excursion
  1645.     (goto-char (point-min))
  1646.     (vc-backend-dispatch buffer-file-name
  1647.      (re-search-forward  "%[MIRLBSDHTEGUYFPQCZWA]%" nil t)    ;; SCCS
  1648.      (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)        ;; RCS
  1649.      )
  1650.     ))
  1651.  
  1652. ;; Back-end-dependent stuff ends here.
  1653.  
  1654. ;; Set up key bindings for use while editing log messages
  1655.  
  1656. (defun vc-log-mode ()
  1657.   "Minor mode for driving version-control tools.
  1658. These bindings are added to the global keymap when you enter this mode:
  1659. \\[vc-next-action]        perform next logical version-control operation on current file
  1660. \\[vc-register]            register current file
  1661. \\[vc-toggle-read-only]        like next-action, but won't register files
  1662. \\[vc-insert-headers]        insert version-control headers in current file
  1663. \\[vc-print-log]        display change history of current file
  1664. \\[vc-revert-buffer]        revert buffer to latest version
  1665. \\[vc-cancel-version]        undo latest checkin
  1666. \\[vc-diff]        show diffs between file versions
  1667. \\[vc-version-other-window]        visit old version in another window
  1668. \\[vc-directory]        show all files locked by any user in or below .
  1669. \\[vc-update-change-log]        add change log entry from recent checkins
  1670.  
  1671. While you are entering a change log message for a version, the following
  1672. additional bindings will be in effect.
  1673.  
  1674. \\[vc-finish-logentry]    proceed with check in, ending log message entry
  1675.  
  1676. Whenever you do a checkin, your log comment is added to a ring of
  1677. saved comments.  These can be recalled as follows:
  1678.  
  1679. \\[vc-next-comment]    replace region with next message in comment ring
  1680. \\[vc-previous-comment]    replace region with previous message in comment ring
  1681. \\[vc-comment-search-reverse]    search backward for regexp in the comment ring
  1682. \\[vc-comment-search-forward]    search backward for regexp in the comment ring
  1683.  
  1684. Entry to the change-log submode calls the value of text-mode-hook, then
  1685. the value of vc-log-mode-hook.
  1686.  
  1687. Global user options:
  1688.     vc-initial-comment    If non-nil, require user to enter a change
  1689.                 comment upon first checkin of the file.
  1690.  
  1691.     vc-keep-workfiles    Non-nil value prevents workfiles from being
  1692.                 deleted when changes are checked in
  1693.  
  1694.         vc-suppress-confirm     Suppresses some confirmation prompts,
  1695.                 notably for reversions.
  1696.  
  1697.     vc-header-alist        Which keywords to insert when adding headers
  1698.                 with \\[vc-insert-headers].  Defaults to
  1699.                 '(\"\%\W\%\") under SCCS, '(\"\$Id\$\") under RCS.
  1700.  
  1701.     vc-static-header-alist    By default, version headers inserted in C files
  1702.                 get stuffed in a static string area so that
  1703.                 ident(RCS) or what(SCCS) can see them in the
  1704.                 compiled object code.  You can override this
  1705.                 by setting this variable to nil, or change
  1706.                 the header template by changing it.
  1707.  
  1708.     vc-command-messages    if non-nil, display run messages from the
  1709.                 actual version-control utilities (this is
  1710.                 intended primarily for people hacking vc
  1711.                 itself).
  1712. "
  1713.   (interactive)
  1714.   (set-syntax-table text-mode-syntax-table)
  1715.   (use-local-map vc-log-entry-mode)
  1716.   (setq local-abbrev-table text-mode-abbrev-table)
  1717.   (setq major-mode 'vc-log-mode)
  1718.   (setq mode-name "VC-Log")
  1719.   (make-local-variable 'vc-log-file)
  1720.   (make-local-variable 'vc-log-version)
  1721.   (make-local-variable 'vc-comment-ring-index)
  1722.   (set-buffer-modified-p nil)
  1723.   (setq buffer-file-name nil)
  1724.   (run-hooks 'text-mode-hook 'vc-log-mode-hook)
  1725. )
  1726.  
  1727. ;; Initialization code, to be done just once at load-time
  1728. (if vc-log-entry-mode
  1729.     nil
  1730.   (setq vc-log-entry-mode (make-sparse-keymap))
  1731.   (define-key vc-log-entry-mode "\M-n" 'vc-next-comment)
  1732.   (define-key vc-log-entry-mode "\M-p" 'vc-previous-comment)
  1733.   (define-key vc-log-entry-mode "\M-r" 'vc-comment-search-reverse)
  1734.   (define-key vc-log-entry-mode "\M-s" 'vc-comment-search-forward)
  1735.   (define-key vc-log-entry-mode "\C-c\C-c" 'vc-finish-logentry)
  1736.   )
  1737.  
  1738. ;;; These things should probably be generally available
  1739.  
  1740. (defun vc-file-tree-walk (func &rest args)
  1741.   "Walk recursively through default directory.
  1742. Invoke FUNC f ARGS on each non-directory file f underneath it."
  1743.   (vc-file-tree-walk-internal default-directory func args)
  1744.   (message "Traversing directory %s...done" default-directory))
  1745.  
  1746. (defun vc-file-tree-walk-internal (file func args)
  1747.   (if (not (file-directory-p file))
  1748.       (apply func file args)
  1749.     (message "Traversing directory %s..." file)
  1750.     (let ((dir (file-name-as-directory file)))
  1751.       (mapcar
  1752.        (function
  1753.     (lambda (f) (or
  1754.              (string-equal f ".")
  1755.              (string-equal f "..")
  1756.              (let ((dirf (concat dir f)))
  1757.             (or
  1758.              (file-symlink-p dirf) ;; Avoid possible loops
  1759.              (vc-file-tree-walk-internal dirf func args))))))
  1760.        (directory-files dir)))))
  1761.  
  1762. (provide 'vc)
  1763.  
  1764. ;;; DEVELOPER'S NOTES ON CONCURRENCY PROBLEMS IN THIS CODE
  1765. ;;;
  1766. ;;; These may be useful to anyone who has to debug or extend the package.
  1767. ;;; 
  1768. ;;; A fundamental problem in VC is that there are time windows between
  1769. ;;; vc-next-action's computations of the file's version-control state and
  1770. ;;; the actions that change it.  This is a window open to lossage in a
  1771. ;;; multi-user environment; someone else could nip in and change the state
  1772. ;;; of the master during it.
  1773. ;;; 
  1774. ;;; The performance problem is that rlog/prs calls are very expensive; we want
  1775. ;;; to avoid them as much as possible.
  1776. ;;; 
  1777. ;;; ANALYSIS:
  1778. ;;; 
  1779. ;;; The performance problem, it turns out, simplifies in practice to the
  1780. ;;; problem of making vc-locking-user fast.  The two other functions that call
  1781. ;;; prs/rlog will not be so commonly used that the slowdown is a problem; one
  1782. ;;; makes snapshots, the other deletes the calling user's last change in the
  1783. ;;; master.
  1784. ;;; 
  1785. ;;; The race condition implies that we have to either (a) lock the master
  1786. ;;; during the entire execution of vc-next-action, or (b) detect and
  1787. ;;; recover from errors resulting from dispatch on an out-of-date state.
  1788. ;;; 
  1789. ;;; Alternative (a) appears to be unfeasible.  The problem is that we can't
  1790. ;;; guarantee that the lock will ever be removed.  Suppose a user starts a
  1791. ;;; checkin, the change message buffer pops up, and the user, having wandered
  1792. ;;; off to do something else, simply forgets about it?
  1793. ;;; 
  1794. ;;; Alternative (b), on the other hand, works well with a cheap way to speed up
  1795. ;;; vc-locking-user.  Usually, if a file is registered, we can read its locked/
  1796. ;;; unlocked state and its current owner from its permissions.
  1797. ;;; 
  1798. ;;; This shortcut will fail if someone has manually changed the workfile's
  1799. ;;; permissions; also if developers are munging the workfile in several
  1800. ;;; directories, with symlinks to a master (in this latter case, the
  1801. ;;; permissions shortcut will fail to detect a lock asserted from another
  1802. ;;; directory).
  1803. ;;; 
  1804. ;;; Note that these cases correspond exactly to the errors which could happen
  1805. ;;; because of a competing checkin/checkout race in between two instances of
  1806. ;;; vc-next-action.
  1807. ;;; 
  1808. ;;; For VC's purposes, a workfile/master pair may have the following states:
  1809. ;;; 
  1810. ;;; A. Unregistered.  There is a workfile, there is no master.
  1811. ;;; 
  1812. ;;; B. Registered and not locked by anyone.
  1813. ;;; 
  1814. ;;; C. Locked by calling user and unchanged.
  1815. ;;; 
  1816. ;;; D. Locked by the calling user and changed.
  1817. ;;; 
  1818. ;;; E. Locked by someone other than the calling user.
  1819. ;;; 
  1820. ;;; This makes for 25 states and 20 error conditions.  Here's the matrix:
  1821. ;;; 
  1822. ;;; VC's idea of state
  1823. ;;;  |
  1824. ;;;  V  Actual state   RCS action              SCCS action          Effect
  1825. ;;;    A  B  C  D  E
  1826. ;;;  A .  1  2  3  4   ci -u -t-          admin -fb -i<file>      initial admin
  1827. ;;;  B 5  .  6  7  8   co -l              get -e                  checkout
  1828. ;;;  C 9  10 .  11 12  co -u              unget; get              revert
  1829. ;;;  D 13 14 15 .  16  ci -u -m<comment>  delta -y<comment>; get  checkin
  1830. ;;;  E 17 18 19 20 .   rcs -u -M ; rcs -l unget -n ; get -g       steal lock
  1831. ;;; 
  1832. ;;; All commands take the master file name as a last argument (not shown).
  1833. ;;; 
  1834. ;;; In the discussion below, a "self-race" is a pathological situation in
  1835. ;;; which VC operations are being attempted simultaneously by two or more
  1836. ;;; Emacsen running under the same username.
  1837. ;;; 
  1838. ;;; The vc-next-action code has the following windows:
  1839. ;;; 
  1840. ;;; Window P:
  1841. ;;;    Between the check for existence of a master file and the call to
  1842. ;;; admin/checkin in vc-buffer-admin (apparent state A).  This window may
  1843. ;;; never close if the initial-comment feature is on.
  1844. ;;; 
  1845. ;;; Window Q:
  1846. ;;;    Between the call to vc-workfile-unchanged-p in and the immediately
  1847. ;;; following revert (apparent state C).
  1848. ;;; 
  1849. ;;; Window R:
  1850. ;;;    Between the call to vc-workfile-unchanged-p in and the following
  1851. ;;; checkin (apparent state D).  This window may never close.
  1852. ;;; 
  1853. ;;; Window S:
  1854. ;;;    Between the unlock and the immediately following checkout during a
  1855. ;;; revert operation (apparent state C).  Included in window Q.
  1856. ;;; 
  1857. ;;; Window T:
  1858. ;;;    Between vc-locking-user and the following checkout (apparent state B).
  1859. ;;; 
  1860. ;;; Window U:
  1861. ;;;    Between vc-locking-user and the following revert (apparent state C).
  1862. ;;; Includes windows Q and S.
  1863. ;;; 
  1864. ;;; Window V:
  1865. ;;;    Between vc-locking-user and the following checkin (apparent state
  1866. ;;; D).  This window may never be closed if the user fails to complete the
  1867. ;;; checkin message.  Includes window R.
  1868. ;;; 
  1869. ;;; Window W:
  1870. ;;;    Between vc-locking-user and the following steal-lock (apparent
  1871. ;;; state E).  This window may never close if the user fails to complete
  1872. ;;; the steal-lock message.  Includes window X.
  1873. ;;; 
  1874. ;;; Window X:
  1875. ;;;    Between the unlock and the immediately following re-lock during a
  1876. ;;; steal-lock operation (apparent state E).  This window may never cloce
  1877. ;;; if the user fails to complete the steal-lock message.
  1878. ;;; 
  1879. ;;; Errors:
  1880. ;;; 
  1881. ;;; Apparent state A ---
  1882. ;;;
  1883. ;;; 1. File looked unregistered but is actually registered and not locked.
  1884. ;;; 
  1885. ;;;    Potential cause: someone else's admin during window P, with
  1886. ;;; caller's admin happening before their checkout.
  1887. ;;; 
  1888. ;;;    RCS: ci will fail with a "no lock set by <user>" message.
  1889. ;;;    SCCS: admin will fail with error (ad19).
  1890. ;;; 
  1891. ;;;    We can let these errors be passed up to the user.
  1892. ;;; 
  1893. ;;; 2. File looked unregistered but is actually locked by caller, unchanged.
  1894. ;;; 
  1895. ;;;    Potential cause: self-race during window P.
  1896. ;;; 
  1897. ;;;    RCS: will revert the file to the last saved version and unlock it.
  1898. ;;;    SCCS: will fail with error (ad19).
  1899. ;;; 
  1900. ;;;    Either of these consequences is acceptable.
  1901. ;;; 
  1902. ;;; 3. File looked unregistered but is actually locked by caller, changed.
  1903. ;;; 
  1904. ;;;    Potential cause: self-race during window P.
  1905. ;;; 
  1906. ;;;    RCS: will register the caller's workfile as a delta with a
  1907. ;;; null change comment (the -t- switch will be ignored).
  1908. ;;;    SCCS: will fail with error (ad19).
  1909. ;;; 
  1910. ;;; 4. File looked unregistered but is locked by someone else.
  1911. ;;; 
  1912. ;;;    Potential cause: someone else's admin during window P, with
  1913. ;;; caller's admin happening *after* their checkout.
  1914. ;;; 
  1915. ;;;    RCS: will fail with a "no lock set by <user>" message.
  1916. ;;;    SCCS: will fail with error (ad19).
  1917. ;;; 
  1918. ;;;    We can let these errors be passed up to the user.
  1919. ;;; 
  1920. ;;; Apparent state B ---
  1921. ;;;
  1922. ;;; 5. File looked registered and not locked, but is actually unregistered.
  1923. ;;; 
  1924. ;;;    Potential cause: master file got nuked during window P.
  1925. ;;; 
  1926. ;;;    RCS: will fail with "RCS/<file>: No such file or directory"
  1927. ;;;    SCCS: will fail with error ut4.
  1928. ;;; 
  1929. ;;;    We can let these errors be passed up to the user.
  1930. ;;; 
  1931. ;;; 6. File looked registered and not locked, but is actually locked by the
  1932. ;;; calling user and unchanged.
  1933. ;;; 
  1934. ;;;    Potential cause: self-race during window T.
  1935. ;;; 
  1936. ;;;    RCS: in the same directory as the previous workfile, co -l will fail
  1937. ;;; with "co error: writable foo exists; checkout aborted".  In any other
  1938. ;;; directory, checkout will succeed.
  1939. ;;;    SCCS: will fail with ge17.
  1940. ;;; 
  1941. ;;;    Either of these consequences is acceptable.
  1942. ;;; 
  1943. ;;; 7. File looked registered and not locked, but is actually locked by the
  1944. ;;; calling user and changed.
  1945. ;;; 
  1946. ;;;    As case 6.
  1947. ;;; 
  1948. ;;; 8. File looked registered and not locked, but is actually locked by another
  1949. ;;; user.
  1950. ;;; 
  1951. ;;;    Potential cause: someone else checks it out during window T.
  1952. ;;; 
  1953. ;;;    RCS: co error: revision 1.3 already locked by <user>
  1954. ;;;    SCCS: fails with ge4 (in directory) or ut7 (outside it).
  1955. ;;; 
  1956. ;;;    We can let these errors be passed up to the user.
  1957. ;;; 
  1958. ;;; Apparent state C ---
  1959. ;;;
  1960. ;;; 9. File looks locked by calling user and unchanged, but is unregistered.
  1961. ;;; 
  1962. ;;;    As case 5.
  1963. ;;; 
  1964. ;;; 10. File looks locked by calling user and unchanged, but is actually not
  1965. ;;; locked.
  1966. ;;; 
  1967. ;;;    Potential cause: a self-race in window U, or by the revert's
  1968. ;;; landing during window X of some other user's steal-lock or window S
  1969. ;;; of another user's revert.
  1970. ;;; 
  1971. ;;;    RCS: succeeds, refreshing the file from the identical version in
  1972. ;;; the master.
  1973. ;;;    SCCS: fails with error ut4 (p file nonexistent).
  1974. ;;;
  1975. ;;;    Either of these consequences is acceptable.
  1976. ;;; 
  1977. ;;; 11. File is locked by calling user.  It looks unchanged, but is actually
  1978. ;;; changed.
  1979. ;;; 
  1980. ;;;    Potential cause: the file would have to be touched by a self-race
  1981. ;;; during window Q.
  1982. ;;; 
  1983. ;;;    The revert will succeed, removing whatever changes came with
  1984. ;;; the touch.  It is theoretically possible that work could be lost.
  1985. ;;; 
  1986. ;;; 12. File looks like it's locked by the calling user and unchanged, but
  1987. ;;; it's actually locked by someone else.
  1988. ;;; 
  1989. ;;;    Potential cause: a steal-lock in window V.
  1990. ;;; 
  1991. ;;;    RCS: co error: revision <rev> locked by <user>; use co -r or rcs -u
  1992. ;;;    SCCS: fails with error un2
  1993. ;;; 
  1994. ;;;    We can pass these errors up to the user.
  1995. ;;; 
  1996. ;;; Apparent state D ---
  1997. ;;;
  1998. ;;; 13. File looks like it's locked by the calling user and changed, but it's
  1999. ;;; actually unregistered.
  2000. ;;; 
  2001. ;;;    Potential cause: master file got nuked during window P.
  2002. ;;; 
  2003. ;;;    RCS: Checks in the user's version as an initial delta.
  2004. ;;;    SCCS: will fail with error ut4.
  2005. ;;;
  2006. ;;;    This case is kind of nasty.  It means VC may fail to detect the
  2007. ;;; loss of previous version information.
  2008. ;;; 
  2009. ;;; 14. File looks like it's locked by the calling user and changed, but it's
  2010. ;;; actually unlocked.
  2011. ;;; 
  2012. ;;;    Potential cause: self-race in window V, or the checkin happening
  2013. ;;; during the window X of someone else's steal-lock or window S of
  2014. ;;; someone else's revert.
  2015. ;;; 
  2016. ;;;    RCS: ci will fail with "no lock set by <user>".
  2017. ;;;    SCCS: delta will fail with error ut4.
  2018. ;;; 
  2019. ;;; 15. File looks like it's locked by the calling user and changed, but it's
  2020. ;;; actually locked by the calling user and unchanged.
  2021. ;;; 
  2022. ;;;    Potential cause: another self-race --- a whole checkin/checkout
  2023. ;;; sequence by the calling user would have to land in window R.
  2024. ;;; 
  2025. ;;;    SCCS: checks in a redundant delta and leaves the file unlocked as usual.
  2026. ;;;    RCS: reverts to the file state as of the second user's checkin, leaving
  2027. ;;; the file unlocked.
  2028. ;;;
  2029. ;;;    It is theoretically possible that work could be lost under RCS.
  2030. ;;; 
  2031. ;;; 16. File looks like it's locked by the calling user and changed, but it's
  2032. ;;; actually locked by a different user.
  2033. ;;; 
  2034. ;;;    RCS: ci error: no lock set by <user>
  2035. ;;;    SCCS: unget will fail with error un2
  2036. ;;; 
  2037. ;;;    We can pass these errors up to the user.
  2038. ;;; 
  2039. ;;; Apparent state E ---
  2040. ;;;
  2041. ;;; 17. File looks like it's locked by some other user, but it's actually
  2042. ;;; unregistered.
  2043. ;;; 
  2044. ;;;    As case 13.
  2045. ;;; 
  2046. ;;; 18. File looks like it's locked by some other user, but it's actually
  2047. ;;; unlocked.
  2048. ;;; 
  2049. ;;;    Potential cause: someone released a lock during window W.
  2050. ;;; 
  2051. ;;;    RCS: The calling user will get the lock on the file.
  2052. ;;;    SCCS: unget -n will fail with cm4.
  2053. ;;; 
  2054. ;;;    Either of these consequences will be OK.
  2055. ;;; 
  2056. ;;; 19. File looks like it's locked by some other user, but it's actually
  2057. ;;; locked by the calling user and unchanged.
  2058. ;;; 
  2059. ;;;    Potential cause: the other user relinquishing a lock followed by
  2060. ;;; a self-race, both in window W.
  2061. ;;; 
  2062. ;;;     Under both RCS and SCCS, both unlock and lock will succeed, making
  2063. ;;; the sequence a no-op.
  2064. ;;; 
  2065. ;;; 20. File looks like it's locked by some other user, but it's actually
  2066. ;;; locked by the calling user and changed.
  2067. ;;; 
  2068. ;;;     As case 19.
  2069. ;;; 
  2070. ;;; PROBLEM CASES:
  2071. ;;; 
  2072. ;;;    In order of decreasing severity:
  2073. ;;; 
  2074. ;;;    Cases 11 and 15 under RCS are the only one that potentially lose work.
  2075. ;;; They would require a self-race for this to happen.
  2076. ;;; 
  2077. ;;;    Case 13 in RCS loses information about previous deltas, retaining
  2078. ;;; only the information in the current workfile.  This can only happen
  2079. ;;; if the master file gets nuked in window P.
  2080. ;;; 
  2081. ;;;    Case 3 in RCS and case 15 under SCCS insert a redundant delta with
  2082. ;;; no change comment in the master.  This would require a self-race in
  2083. ;;; window P or R respectively.
  2084. ;;; 
  2085. ;;;    Cases 2, 10, 19 and 20 do extra work, but make no changes.
  2086. ;;; 
  2087. ;;;    Unfortunately, it appears to me that no recovery is possible in these
  2088. ;;; cases.  They don't yield error messages, so there's no way to tell that
  2089. ;;; a race condition has occurred.
  2090. ;;; 
  2091. ;;;    All other cases don't change either the workfile or the master, and
  2092. ;;; trigger command errors which the user will see.
  2093. ;;; 
  2094. ;;;    Thus, there is no explicit recovery code.
  2095.  
  2096. ;;; vc.el ends here
  2097.